const lotteryCode = window.localStorage.getItem('lottery-code');
const lotteryCodeArray = lotteryCode ? JSON.parse(lotteryCode) : [];

const lottery = window.localStorage.getItem('lottery');
const lotteryArray = lottery ? JSON.parse(lottery) : [];

const data = {
    lotteryCode: lotteryCodeArray, // 参加人
    lotteryResult: lotteryArray // 奖项
};

/* 人数设置 */
/**
 * 获取名单号码
 */
export function getLotteryCode() {
    return data.lotteryCode;
}

/**
 * 追加中奖名单
 * @param {*} num 
 */
export function appendLotteryCode(num = 0, filter = []) {
    const newData = new Array(num)
        .fill(0)
        .map((_, i) => {
            return {
                key: i + 1,
                // name: `张${i + 1}`,
                // photo: 'https://imgcdn.huanjutang.com/assets/qrcode/20201105/5fa3a627f1d42.jpg'
            };
        })
        .filter(item => filter.indexOf(String(item.key)) === -1);
    data.lotteryCode.push(...newData);
    window.localStorage.setItem('lottery-code', JSON.stringify(data.lotteryCode));
}

/**
 * 追加中奖名单
 * @param {*} newData 
 */
export function pushLotteryCode(newData = []) {
    data.lotteryCode.push(...newData);
    window.localStorage.setItem('lottery-code', JSON.stringify(data.lotteryCode));
}

/**
 * 设置中奖名单
 * @param {*} num 
 */
export function setLotteryCode(result = []) {
    data.lotteryCode = result;
    window.localStorage.setItem('lottery-code', JSON.stringify(data.lotteryCode));
}

export function resetLotteryCode() {
    data.lotteryCode = [];
    window.localStorage.removeItem('lottery-code');
}

/* 奖项设置 */
/**
 * 获取奖项
 */
export function getLottery() {
    return data.lotteryResult;
}

/**
 * 追加奖项
 * @param {*} name 奖项名称
 * @param {*} num 中奖人数
 */
export function addLottery(name, num) {
    if (!name) {
        throw new Error('奖项名称不能为空');
    }

    if (num < 1 || data.lotteryCode.length < num) {
        throw new Error('奖项人数不能小于0或大于剩余人数');
    }

    data.lotteryResult.push({
        isUse: false,   // 是否已经使用
        name: name, // 奖项名称
        number: num,    // 中奖数量
        key: Math.floor(Math.random() * 10000), // 奖项ID
        result: []  // 中奖结果
    });
    window.localStorage.setItem('lottery', JSON.stringify(data.lotteryResult));
}

/**
 * 设置奖项
 * @param {*} result 
 */
export function setLottery(result) {
    data.lotteryResult = result;
    window.localStorage.setItem('lottery', JSON.stringify(data.lotteryResult));
}

/**
 * 重置奖项
 */
export function resetLottery() {
    data.lotteryResult.forEach(item => {
        const lotteryCode = item.result;
        if (lotteryCode.length > 0) {
            pushLotteryCode(item.result);
        }
    });
    data.lotteryResult = [];
    window.localStorage.removeItem('lottery');
}

/**
 * 获取中奖结果
 * @param {*} key 奖项key
 * @returns {undefined|Object}
 */
export function getLotteryResult(key) {
    return data.lotteryResult.find(item => item.key === key);
}

/**
 * 设置中奖结果
 * @param {*} key 奖项key
 * @param {*} result 设置的结果
 */
export function setLotteryResult(key, result = []) {
    const newResult = data.lotteryResult.map(item => {
        return {
            ...item,
            result: item.key === key ? result : item.result
        }
    });
    setLottery(newResult);
}

/**
 * 重置中奖结果
 * @param {Boolean} isAll 是否重置所有
 * @param {*} key 如果不是重置所有就需要传入奖项key
 */
export function resetLotteryResult(isAll = true, key = undefined) {
    if (isAll) {
        const result = data.lotteryResult.map(item => {
            const lotteryCode = item.result;
            if (lotteryCode.length > 0) {
                pushLotteryCode(item.result);
            }

            return {
                ...item,
                result: []
            };
        });
        setLottery(result);
    } else {
        const item = data.lotteryResult.find(item => item.key === key);
        pushLotteryCode(item.result);
        setLotteryResult(key, []);
    }
}